import axios from 'axios'
import { useUserStore } from '@/stores/userStore'

// 根据环境变量设置 baseURL
// 创建 axios 实例
const http = axios.create({
  baseURL: '/store', // 替换为实际的 API 基础 URL
  timeout: 10000, // 请求超时时间
})

// 请求拦截器
http.interceptors.request.use(
  (config) => {
    const tokenStore = useUserStore()
    if (tokenStore.token) {
      config.headers.Authorization = `Bearer ${tokenStore.token}`
    }
    return config
  },
  (error) => {
    return Promise.reject(error)
  },
)

http.interceptors.response.use(
  (response) => {
    console.log('response', typeof response)
    return response.data
  },
  (error) => {
    return Promise.reject(error)
  },
)

export default http
